<template>
  <d2-container>
    <el-card shadow="never" class="d2-mb">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="日期">
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="formInlinedates"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="用户">
          <el-select v-model="formInline.creator">
            <el-option
              v-for="item in user_data"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日志类型">
          <el-select v-model="formInline.log_type">
            <el-option
              v-for="item in type_data"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="formInline.content"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="select_fn()"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </el-card>
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      :cell-style="{ 'text-align': 'center' }"
      :header-cell-style="{ background: '#eef1f6','text-align': 'center' }"
    >
      <el-table-column prop="modified_time" label="时间" sortable>
        <template slot-scope="scope">{{
          scope.row.modified_time | dateYMDHMSFormat_ss
        }}</template>
      </el-table-column>
      <el-table-column prop="creator.name" label="用户" sortable> </el-table-column>
      <el-table-column prop="log_type" label="类型" sortable>
        <template slot-scope="scope">
          {{ type_data[scope.row.log_type].label }}
        </template>
      </el-table-column>
      <el-table-column prop="content" label="内容" sortable> </el-table-column>
    </el-table>
    <el-pagination
      @current-change="handlePaginationChange"
      :current-page="formInline.p"
      layout="total, prev, pager, next"
      :total="formInline.count"
    >
    </el-pagination>
  </d2-container>
</template>
<script>
import { getLog, getUsers } from "@/api/url";
export default {
  data() {
    return {
      formInlinedates: "",
      formInline: {
        creator: "",
        log_type: "",
        content: "",
        p: 1,
        count: 0,
        pageSize: 20,
        startDate: "",
        endDate: ""
      },
      input: "",
      value1: "",
      tableData: [],
      user_data: [],
      type_data: [{ label: "登陆", value: 0 }]
    };
  },
  created() {
    this.select_list_fn();
    this.select_user_fn();
  },
  methods: {
    select_list_fn() {
      getLog(this.formInline).then(res => {
        this.formInline.count = res.result.count;
        this.tableData = res.result.results;
      });
    },
    select_user_fn() {
      getUsers().then(res => {
        this.user_data = res.result;
      });
    },
    handlePaginationChange(val) {
      this.formInline.p = val;
      this.select_list_fn();
    },
    select_fn() {
       this.formInline.startDate = "";
        this.formInline.endDate = "";
      if (this.formInlinedates) {
        this.formInline.startDate = this.formInlinedates[0];
        this.formInline.endDate = this.formInlinedates[1];
      }
      this.formInline.p = 1;
      this.select_list_fn();
    }
  }
};
</script>
